package com.ruoyi.system.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.WaterMeterrepairinfo;
import com.ruoyi.system.domain.WaterWorkOrder;
import com.ruoyi.system.mapper.WaterMeterrepairinfoMapper;
import com.ruoyi.system.service.IWaterMeterrepairinfoService;
import com.ruoyi.system.service.IWaterWorkOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 表具维修Service业务层处理
 *
 * @author ruoyi
 * @date 2025-09-17
 */
@Service
public class WaterMeterrepairinfoServiceImpl implements IWaterMeterrepairinfoService
{
    @Autowired
    private WaterMeterrepairinfoMapper waterMeterrepairinfoMapper;

    @Autowired
    private IWaterWorkOrderService waterWorkOrderService;

    /**
     * 查询表具维修
     *
     * @param id 表具维修主键
     * @return 表具维修
     */
    @Override
    public WaterMeterrepairinfo selectWaterMeterrepairinfoById(Long id)
    {
        return waterMeterrepairinfoMapper.selectWaterMeterrepairinfoById(id);
    }

    /**
     * 查询表具维修列表
     *
     * @param waterMeterrepairinfo 表具维修
     * @return 表具维修
     */
    @Override
    public List<WaterMeterrepairinfo> selectWaterMeterrepairinfoList(WaterMeterrepairinfo waterMeterrepairinfo)
    {
        return waterMeterrepairinfoMapper.selectWaterMeterrepairinfoList(waterMeterrepairinfo);
    }

    @Override
    public List<WaterMeterrepairinfo> selectWaterMeterrepairinfoListPage(String depid,String uId, String userid, String installState, String username, String useraddress, String meterNo, Integer beginSize, Integer endSize) {
        return waterMeterrepairinfoMapper.selectWaterMeterrepairinfoListPage(depid,uId,userid,installState,username,useraddress,meterNo, beginSize, endSize);
    }

    /**
     * 新增表具维修
     *
     * @param waterMeterrepairinfo 表具维修
     * @return 结果
     */
    @Override
    public int insertWaterMeterrepairinfo(WaterMeterrepairinfo waterMeterrepairinfo)
    {
        WaterMeterrepairinfo waterMeterrepairinfoSelect = new WaterMeterrepairinfo();
        waterMeterrepairinfoSelect.setOrderNumber(waterMeterrepairinfo.getOrderNumber());

        List<WaterMeterrepairinfo> waterMeterrepairinfoNum = waterMeterrepairinfoMapper.selectWaterMeterrepairinfoList(waterMeterrepairinfoSelect);
        if(waterMeterrepairinfoNum.size()>0){
            return 0;
        }
        waterMeterrepairinfo.setCreateTime(DateUtils.getNowDate());
        int repairResult =waterMeterrepairinfoMapper.insertWaterMeterrepairinfo(waterMeterrepairinfo);
        try {
            if(repairResult>0){
                WaterWorkOrder waterWorkOrder = new WaterWorkOrder();
                String orderNumber = waterMeterrepairinfo.getOrderNumber();

                waterWorkOrder.setOrderNumber(orderNumber);
                waterWorkOrder.setStatus(2L);
                //更新工单信息
                waterWorkOrderService.updateWaterWorkOrderNumber(waterWorkOrder);
            }
        }catch (Exception e){
            throw new RuntimeException("更多信息更新时发生错误：" + e.getMessage(), e);
        }
        return repairResult;
    }

    /**
     * 修改表具维修
     *
     * @param waterMeterrepairinfo 表具维修
     * @return 结果
     */
    @Override
    public int updateWaterMeterrepairinfo(WaterMeterrepairinfo waterMeterrepairinfo)
    {
        waterMeterrepairinfo.setUpdateTime(DateUtils.getNowDate());
        return waterMeterrepairinfoMapper.updateWaterMeterrepairinfo(waterMeterrepairinfo);
    }
    @Override
    public int updateWaterMeterrepairinfoNumber(WaterMeterrepairinfo waterMeterrepairinfo)
    {
        waterMeterrepairinfo.setUpdateTime(DateUtils.getNowDate());
        return waterMeterrepairinfoMapper.updateWaterMeterrepairinfoNumber(waterMeterrepairinfo);
    }

    /**
     * 批量删除表具维修
     *
     * @param ids 需要删除的表具维修主键
     * @return 结果
     */
    @Override
    public int deleteWaterMeterrepairinfoByIds(Long[] ids)
    {
        return waterMeterrepairinfoMapper.deleteWaterMeterrepairinfoByIds(ids);
    }

    /**
     * 删除表具维修信息
     *
     * @param id 表具维修主键
     * @return 结果
     */
    @Override
    public int deleteWaterMeterrepairinfoById(Long id)
    {
        return waterMeterrepairinfoMapper.deleteWaterMeterrepairinfoById(id);
    }
}
